﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace BLL {
    public class ContactGroup {
        SQLDAL.ContactGroup group = new SQLDAL.ContactGroup();
        public DataTable GetList(string strWhere) {
            return group.GetList(strWhere);
        }
        public Model.ContactGroup GetModel(string groupName) {
            return group.GetModel(groupName);
        }
        public Model.ContactGroup GetModel(int Id) {
            return group.GetModel(Id);
        }
        public bool CheckModel(Model.ContactGroup model, out string msg) {
            bool check = true;
            msg = "";
            if (model.GroupName == "") {
                msg = "分组名称不能为空。";
                check = false;
            }
            return check;
        }
        public bool Add(Model.ContactGroup model,out string msg) {
            if (!CheckModel(model,out msg)) {
                return false;
            }
            else {
                Model.ContactGroup modelcheck = GetModel(model.GroupName);
                if (modelcheck != null) {
                    msg = "分组名称不能为空。";
                    return false;
                }
                else {
                    return group.Add(model);
                }
            }
        }
        public bool Delete(int Id) {
            SQLDAL.Contact contact = new SQLDAL.Contact();
            int count = contact.GetContactCountByGroupId(Id);
            if (count > 0) {
                return false;
            }
            else {
                return group.Delete(Id);
            }
        }
        //更新时，未判断名称是否已存在
        public bool Update(Model.ContactGroup model,out string msg) {
            if (!CheckModel(model, out msg))
                return false;
            return group.Update(model);
        }
    }
}
